//
//  ViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/1.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  pch文件

import Foundation

// MARK:- 授权的常量
//let app_key = "wxb4ba3c02aa476ea1"
//微信app id
let app_key = "wxd6870b76bf49dd4c"
//微信密匙
let appSrcret = "8765965abd382bd6216b230b11bc3bb1"

//QQ密匙
let QQAppID = "1105441328"

		
let WXPatient_App_ID = app_key
let WXPatient_App_Secret = appSrcret
let WX_ACCESS_TOKEN = "access_token"
let WX_OPEN_ID = "openid"
let WX_REFRESH_TOKEN = "refresh_token"
let WX_UNION_ID = "unionid"
let WX_BASE_URL = "https://api.weixin.qq.com/sns"

let urlPrefix = "http://yungou.ie1e.com/ifs/"

// MARK:- 请求地址
///发送验证码
let sendMsgUrl = "\(urlPrefix)\("sendcode.ashx")"

///注册reg.ashx
let rigisterUrl = "\(urlPrefix)\("reg.ashx")"

///忘记密码pwdfind.ashx
let forgetPassUrl = "\(urlPrefix)\("pwdfind.ashx")"


///登陆login.ashx
let loginUrl = "\(urlPrefix)\("login.ashx")"

///收货地址address.ashx
let addressUrl = "\(urlPrefix)\("address.ashx")"

///修改密码pwdchange.ashx
let xgPass = "\(urlPrefix)\("pwdchange.ashx")"

///退出接口logout.ashx
let logoutURL = "\(urlPrefix)\("logout.ashx")"

///第三方接口oauth.ashx
let oauthURL = "\(urlPrefix)\("oauth.ashx")"

///设备ID，暂时未定值
let deviceID = "zdx123"

///设备类型设备类型 0：Android 1：IOS
let deviceTpye = 1


// MARK:- 提示语
//提示语
let tfNumIsNull = "手机号不能为空"
let tfNumNotCor = "手机号格式不对"
let tfPassNull = "密码不能为空"
let tfAutoNull = "验证码不能为空"
let passTwoChekc = "两次密码不一致"
let tfAuthWrong = "验证码错误"
let loginSuccess = "恭喜您登录成功"
let changePassSuccess = "密码修改成功"
let rigSuccess = "注册成功，恭喜您成为一元预购的新成员"
let msgSengWrong = "发送失败,请重试"
let passAndAccountNot = "帐号与密码不匹配"
let numUsed = "手机号已被注册"
let msgSended = "短信发送成功"
let confirPassNotNull = "确认密码不能为空"
let authNumWrong = "短信验证码错误"
let netWrong = "网络异常,请检查设置"
let agressLawing = "请同意协议"
let changePasSuccess = "你的密码修改成功"
let passNotCor = "原始密码错误"
let confirmPassNull = "确认密码为空"
let passNewNil = "新密码不能为空"
let tokenNotifi = "tokenNotifi"
let phoneNotExist = "手机号不存在"
let loginError = "该账号已在异地登录，请重新登录"
let addressAddSuccess = "添加地址成功"
let addresUpdateSuccess = "修改地址成功"
let changeAddressFail = "尝试修改地址失败"


// MARK:- 第三方登陆返回信息
//qq openID
let qqopenID = "qqopenID"

//qq headimg
let qqHeadImg = "qqHeadImg"

//qq nickName
let qqNickName = "qqNickName"


//微信 openID
let wxopenID = "wxopenID"

//微信头像地址
let wxHeadImgURL = "wxHeadImgURL"

//微信昵称
let wxNickName = "wxNickName"

//第三方平台
enum thirdLoginType : Int {
    case qq
    case weixin
    case weibo
}


// MARK:- 本地信息存储
//用户动态标识ID
let userToken = "userToken"

//本地存储
let localSave = UserDefaults.standard

//同意协议
let agreeLaw = "agreeLaw"

//表头视图tag -- 头像
let myHeadImgTag = 112
//表头视图tag -- 账号
let myHeadTelTag = 111
//退出tag
let myheadLogout = 113


//登陆用户名
let localName = "localName"
//登陆用户名密码
let localPass = "localPass"


// MARK:- 第三方授权后的信息
let u_qName = "u_qName"

//qq授权后的图片地址
let u_qheadImgURL = "u_qheadImgURL"

//微信授权后的名称
let wx_qName = "wx_qName"

//微信授权后的图片地址
let wx_qheadImgURL = "wx_qheadImgURL"


// MARK:- 第三方登陆通知
//qq登陆通知名称
let qqNotifi = "qqNotifi"

//微信登陆通知名称
let wxNotifi = "wxNotifi"

//退出登陆
let logoutNotifi = "logout"


//POST请求
func postWithPath(path: String,paras: Dictionary<String,Any>?,success: @escaping ((_ result: Any) -> ()),failure: @escaping ((_ error: Error) -> ())) {
    
    
    //(1）设置请求路径
    let url:NSURL = NSURL(string:path)!//不需要传递参数
    
    //(2) 创建请求对象
    let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL) //默认为get请求
    request.timeoutInterval = 5.0 //设置请求超时为5秒
    request.httpMethod = "POST"  //设置请求方法
    
    
    //设置请求体
    //拼接URL
    var i = 0
    var address: String = ""
    
    if let paras = paras {
        
        for (key,value) in paras {
            
            if i == 0 {
                
                address += "\(key)=\(value)"
            }else {
                
                address += "&\(key)=\(value)"
            }
            
            i += 1
        }
    }
    
    //把拼接后的字符串转换为data，设置请求体
    request.httpBody = address.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
    
    //(3) 发送请求
    NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue:OperationQueue()) { (res, data, error)in
        //返回主线程执行
        DispatchQueue.main.sync {
            
            if let data = data {
                
                if let result = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) {
                    
                    success(result)
                }
                
            }else {
                failure(error!)
            }
            
        }
    }
    
    
}









